#pragma once
#include "log.hpp"
#include <cstdlib>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
namespace Server
{
    enum
    {
        USAGE_ERR = 1,
        SOCKET_ERR,
        BIND_ERR,
        LISTEN_ERR
    };
    static const uint16_t gport = 8080;
    static const int gbacklog = 5;
    class TcpServer
    {
    public:
        TcpServer(const uint16_t &port = gport)
            : _listensock(-1), _port(port)
        {
        }
        void initServer()
        {
            _listensock = socket(AF_INET, SOCK_STREAM, 0);
            if (_listensock < 0)
            {
                logMessage(FATAL, "create socket error");
                exit(SOCKET_ERR);
            }
            logMessage(NORMAL, "create socket success");
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_addr.s_addr = htonl(INADDR_ANY);
            local.sin_port = htons(_port);
            if (bind(_listensock, (struct sockaddr *)&local, sizeof(local)) < 0)
            {
                logMessage(FATAL, "bind socket error");
                exit(BIND_ERR);
            }
            logMessage(NORMAL, "bind socket success");
            if (listen(_listensock, gbacklog) < 0)
            {
                logMessage(FATAL, "listen socket error");
                exit(LISTEN_ERR);
            }
            logMessage(NORMAL, "listen socket success");
        }
        void start()
        {
            signal(SIGCHLD, SIG_IGN);
            for (;;)
            {
                struct sockaddr_in client;
                memset(&client, 0, sizeof(client));
                socklen_t len = sizeof(client);
                int sock = accept(_listensock, (struct sockaddr *)&client, &len);
                if (sock < 0)
                {
                    logMessage(ERROR, "accept error, next");
                    continue;
                }
                logMessage(NORMAL, "accept a new link success");
                std::cout << "sock : " << sock << std::endl;
                // pid_t pid = fork();
                // if (pid == 0)
                // {
                //     close(_listensock);
                //     if (fork() > 0)
                //     {
                //         exit(0);
                //     }
                //     serviceIO(sock);
                //     close(sock);
                //     exit(0);
                // }
                // pid_t ret = waitpid(pid, nullptr, 0);
                // if (ret > 0)
                // {
                //     std::cout << "waitsuccess: " << ret << std::endl;
                // }
                // close(sock);
                pid_t pid = fork();
                if (pid == 0)
                {
                    close(_listensock);
                    serviceIO(sock);
                    close(sock);
                    exit(0);
                }
                close(sock);
            }
        }
        void serviceIO(int sock)
        {
            char buffer[1024];
            while (true)
            {
                ssize_t s = read(sock, buffer, sizeof(buffer) - 1);
                if (s > 0)
                {
                    buffer[s] = 0;
                    std::cout << "recv message : " << buffer << std::endl;
                    std::string outbuffer = buffer;
                    outbuffer += " server[echo]";
                    write(sock, outbuffer.c_str(), outbuffer.size());
                }
                else if (s == 0)
                {
                    logMessage(NORMAL, "client quit, me too!");
                    break;
                }
            }
        }
        ~TcpServer()
        {
        }

    private:
        int _listensock;
        uint16_t _port;
    };
}